<?php
/**
 * Description:
 * Author: hezhizheng
 * Date: 2018/11/15
 * Time: 9:57
 * Created by Created by Panxsoft.
 */

namespace App\Services\Customers\ResetPwd;

use App\Models\Customers\Customer;
use App\Services\Mailer\Customer\PasswordResetMail;
use App\Services\Utils\ApiChecker;

class ResetPwdForEmailService
{
    private $customer;

    use ApiChecker;

    public function __construct()
    {
        $this->customer = new Customer;
    }

    /**
     * @param Customer $customer
     *
     * @return array
     * @throws \Exception
     */
    public function resetPwd(Customer $customer)
    {
        $find_customer = $this->customer->where('email', $customer->email)
            ->first();
        $this->checkItemExist($find_customer);
        $this->checkIsTrue($find_customer->status === Customer::STATUS_ENABLE, trans('customer.disabled'));

        $new_pwd = Customer::randomDefaultPwd();

        $update = $find_customer->update([
            'password' => bcrypt($new_pwd)
        ]);

        PasswordResetMail::send($customer->email, $new_pwd);

        if (config('app.debug')) {
            return compact('update', 'new_pwd');
        } else {
            return compact('update');
        }
    }
}